
<template>
  <div class="dashboard-container">
    <el-drawer ref="drawer" size="600px" :title="status==1?'新增':'编辑'" :visible.sync="showDialog" :wrapper-closable="true" @close="close">
      <div class="detial">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm">
          <el-form-item label="航班号:" prop="flight_number">
            <el-input v-model="ruleForm.flight_number" placeholder="请输入航班号" />
          </el-form-item>
          <el-form-item label="运输公司:" prop="transport_company">
            <el-input v-model="ruleForm.transport_company" placeholder="请输入运输公司" />
          </el-form-item>
          <el-form-item label="发出地:" prop="start_country">
            <el-input v-model="ruleForm.start_country" placeholder="请输入发出地" />
          </el-form-item>
          <el-form-item label="口岸:" prop="port">
            <el-select
              v-model="ruleForm.port"
              style="width:100%"
              placeholder="请选择走件口岸"
            >
              <el-option
                v-for="(item,index) in portList"
                :key="index"
                :label="item.port_name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="运输方式:" prop="trans_type">
            <el-select
              v-model="ruleForm.trans_type"
              style="width:100%"
              placeholder="请选择运输方式"
            >
              <el-option
                label="海运"
                :value="1"
              />
              <el-option
                label="空运"
                :value="2"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="业务类型:" prop="order_type">
            <el-select
              v-model="ruleForm.order_type"
              style="width:100%"
              placeholder="请选择业务类型"
            >
              <el-option
                label="一般贸易"
                value="MY"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="计划开始时间:" prop="depart_time">
            <el-time-picker
              v-model="ruleForm.depart_time"
              editable
              style="width:100%"
              value-format="HH:mm:ss"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              placeholder="选择计划开始时间"
              :picker-options="startTimeOptions"
            />
          </el-form-item>
          <el-form-item label="计划结束时间:" prop="arrive_time">
            <el-time-picker
              v-model="ruleForm.arrive_time"
              editable
              style="width:100%"
              value-format="HH:mm:ss"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              placeholder="选择计划开始时间"
              :picker-options="startTimeOptions"
            />
          </el-form-item>
          <el-form-item label="到达日期:" prop="arrive_day">
            <el-select
              v-model="ruleForm.arrive_day"
              style="width:100%"
              placeholder="请选择到达日期"
            >
              <el-option
                label="当日到达"
                :value="0"
              />
              <el-option
                label="T+1"
                :value="1"
              />
              <el-option
                label="T+2"
                :value="2"
              />
              <el-option
                label="T+3"
                :value="3"
              />
              <el-option
                label="T+4"
                :value="4"
              />
              <el-option
                label="T+5"
                :value="5"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="出发港口(机场):" prop="port_start">
            <el-input v-model="ruleForm.port_start" placeholder="请输入出发港口(机场)" />
          </el-form-item>
          <el-form-item label="到达港口(机场):" prop="port_end">
            <el-input v-model="ruleForm.port_end" placeholder="请输入到达港口(机场)" />
          </el-form-item>

          <el-form-item label="状态:" prop="status">
            <el-select
              v-model="ruleForm.status"
              style="width:100%"
              placeholder="请选择状态"
            >

              <el-option
                label="启用"
                :value="1"
              />
              <el-option
                label="禁用"
                :value="2"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div class="buttons">
        <el-button
          v-if="status == 1"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="addNews()"
        >
          新 增
        </el-button>
        <el-button
          v-if="status == 2"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="editNews()"
        >
          保 存
        </el-button>
        <el-button
          size="mini"
          type=""
          @click="close()"
        >
          取消
        </el-button>

      </div>

    </el-drawer>
  </div>
</template>

<script>
import { createFlight, updateFlight } from '@/api/lading/index'
import { listPort } from '@/api/base/port'

export default {
  name: 'Detial',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detial: {
      require: false,
      type: Object,
      default: () => {

      }
    },
    status: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      time: '',
      showDialog: false,
      portList: [],
      ruleForm: {
        'flight_number': '',
        'transport_company': '',
        'depart_time': '',
        'arrive_time': '',
        'arrive_day': '',
        'port_start': '',
        'port_end': '',
        'status': '',
        'start_country': '',
        'port': '',
        'trans_type': '',
        'order_type': ''
      },
      rules: {
        flight_number: [
          { required: true, message: '请输入航班号', trigger: 'blur' }
        ],
        trans_type: [
          { required: true, message: '请选择运输方式', trigger: 'blur' }
        ],
        order_type: [
          { required: true, message: '请选择业务类型', trigger: 'blur' }
        ],
        start_country: [
          { required: true, message: '请输入发出地', trigger: 'blur' }
        ],
        port: [
          { required: true, message: '请选择口岸', trigger: 'blur' }
        ],
        transport_company: [
          { required: true, message: '请输入运输公司' }
        ],
        depart_time: [
          { required: true, message: '选择计划开始时间' }
        ],
        arrive_time: [
          { required: true, message: '选择计划开始时间' }
        ],
        arrive_day: [
          { required: true, message: '请选择到达日期' }
        ],

        port_start: [
          { required: true, message: '请输入出发港口(机场)' }
        ],
        port_end: [
          { required: true, message: '请输入到达港口(机场)' }
        ],
        status: [
          { required: true, message: '请选择状态' }
        ]
      }
    }
  },
  watch: {
    visible(val) {
      this.showDialog = this.visible
      if (!this.showDialog) {
        this.resetForm()
      } else {
        if (this.status === 2) {
          // this.ruleForm = this.detial
          Object.keys(this.ruleForm).forEach(i => {
            this.ruleForm[i] = this.detial[i]
          })
          this.ruleForm.id = this.detial.id
        }
      }
    }
  },
  mounted() {
    this.getPort()
  },
  methods: {
    // 口岸列表
    getPort() {
      listPort({
        limit: 1000,
        status: 1
      }).then(response => {
        this.portList = response.data.data
      })
    },
    startTimeOptions() {
      return { selectableRange: '00:00:00 - 23:59:59' }
    },
    chengeTime() {
      if (this.time === '1') {
        this.ruleForm.service_time_start = 5
        this.ruleForm.service_time_end = 7
      } else {
        this.ruleForm.service_time_start = 15
        this.ruleForm.service_time_end = 15
      }
    },
    close() {
      this.$emit('update', false)
    },
    resetForm() {
      this.$refs['ruleForm'].resetFields()
    },
    addNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          createFlight(this.ruleForm).then(res => {
            this.$message.success('添加成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    },
    editNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          updateFlight(this.ruleForm).then(res => {
            this.$message.success('编辑成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    }

  }

}
</script>
<style scoped>
.detial{
  padding:40px 40px 20px 20px;
}
.buttons{
  display: flex;
  flex-direction: row-reverse;
  padding-right: 40px;
}
</style>
